import ContentView from '@/layouts/ContentView'
import RouteView from '@/layouts/RouteView'

// 前端路由表
const constantRouterComponents = {
  resource_biology: () => import('@/pages/resource/views/biology/List.vue'), // 生物特征库
  resource_videos: () => import('@/pages/resource/views/videos/List.vue'), // 视频资源库
  resource_videos_violations: () => import('@/pages/resource/views/videos/Voilate.vue'), // 违规视频库
  resource_videos_law: () => import('@/pages/resource/views/videos/Law.vue'), // 违规视频库
  resource_imageRes: () => import('@/pages/resource/views/imageRes/List.vue'), // 图片资源库
  resource_pointPerson: () => import('@/pages/resource/views/pointPerson/List.vue'), // 重点人员库

  // 远程查床
  remote_task: () => import('@/pages/remote/views/common/List.vue'),
  remote_taskIssue: () => import('@/pages/remote/views/common/List.vue'),
  remote_taskToExamine: () => import('@/pages/remote/views/audit/List.vue'),
  remote_task_add: () => import('@/pages/remote/views/add/Form.vue'),
  remote_task_info: () => import('@/pages/remote/views/common/Info.vue'),

  // 设备视频库
  deviceManage_register: () => import('@/pages/deviceManage/views/register/List.vue'),
  deviceManage_register_save: () => import('@/pages/deviceManage/views/register/Form.vue'),
  deviceManage_maintain: () => import('@/pages/deviceManage/views/maintain/List.vue'),

  // 场景智能监控
  sceneMonitor_hospitalizedScene: () => import('@/pages/sceneMonitor/views/hospitalizedScene/List.vue'),
  sceneMonitor_hospitalizedScene_dept: () => import('@/pages/sceneMonitor/views/hospitalizedScene/DeptList.vue'),
  sceneMonitor_hospitalizedScene_dept_personnel: () => import('@/pages/sceneMonitor/views/hospitalizedScene/PersonnelList.vue'),

  holographicData_realtime: () => import('@/pages/holographicData/views/monitor/RealTimeMinitor.vue'), // 全息数据-实时预览
  holographicData_monitorWarning: () => import('@/pages/holographicData/views/warning/RealTimeWarn.vue'), // 全息数据-实时预警
  holographicData_authenticationData: () => import('@/pages/holographicData/views/recogData/RealtimeDataBoard.vue'), // 全息数据-认证数据看板

  // 预警管理
  warning_infomation: () => import('@/pages/warning/views/infomation/Infomation.vue'),
  warning_rule: () => import('@/pages/warning/views/rule/Rule.vue')
}

const titles = {
  resource_biology: '生物特征库',
  resource_videos: '视频资源库',
  resource_videos_law: '执法视频库',
  resource_videos_violations: '违规视频库',
  resource_imageRes: '图片视频库',
  resource_monitorRecord: '监控记录',
  resource_monitorRecord_contrastCure: '诊疗场景',
  resource_monitorRecord_outpatientScene: '门诊场景',
  resource_monitorRecord_buyMedicine: '购药场景',
  resource_monitorRecord_operationPerson: '执业人员监控',
  resource_monitorRecord_hospitalizedScene: '住院场景',
  resource_pointPerson: '重点人员库',
  resource_pointPerson_pointBlack: '疑似违规人员',
  resource_pointPerson_pointSleeveBrush: '套刷人员',
  resource_pointPerson_pointDoctor: '医师药师',
  resource_pointPerson_warning: '预警人员',

  holographicData_realtime: '实时预览（实时监控）',
  holographicData_monitorWarning: '监控-实时预警',
  holographicData_authenticationData: '认证数据看板'

}

// 前端未找到页面路由（固定不用改）
const notFoundRouter = {
  path: '*', redirect: '/404', hidden: true
}

export const generatorDynamicRouter = (tree, chunk = '') => {
  console.log(chunk)
  const childrenNav = []
  // 根级菜单
  generatorRoutes(tree, childrenNav, chunk)
  return [childrenNav[0], notFoundRouter]
}

/**
 * 格式化树形结构数据 生成 vue-router 层级路由表
 *
 * @param routerMap
 * @param parent
 * @returns {*}
 */
export const generatorRoutes = (list, childrenNav, chunk) => {
  list.map(item => {
    const { path, resuId, parentName } = item
    const currentRouter = {
      key: resuId,
      // 如果路由设置了 path，则作为默认 path，否则 路由地址 动态拼接生成如 /dashboard/workplace
      path: path,
      // 路由名称，建议唯一
      name: resuId || '',
      // 该路由对应页面的 组件 :方案2 (动态加载)
      component: item.name === chunk ? ContentView : (constantRouterComponents[resuId] || RouteView),

      // meta: 页面标题, 页面权限(供指令权限用，可去掉)
      meta: {
        title: titles[resuId] || '',
        permissionId: item.resuId,
        name: item.name,
        pid: parentName
      }
    }
    // 是否有子菜单，并递归处理
    if (item.children && item.children.length > 0) {
      // Recursion
      currentRouter.children = generatorRoutes(item.children, [], currentRouter, chunk)
    }
    // 重定向, 判断是否有子路由，并且当前路由没有对应组件
    if (currentRouter.children && currentRouter.children.length > 0 && !constantRouterComponents[resuId]) {
      currentRouter.redirect = currentRouter.children[0].path
    }
    childrenNav.push(currentRouter)
  })
  return childrenNav
}
